<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>俄罗斯方块</title>
    <style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            background: #f0f0f0;
        }
        canvas { 
            border: 2px solid #333;
            background: #fff; 
        }
        #score { 
            font-size: 24px; 
            margin: 10px; 
            font-family: Arial;
            color: #333;
        }
        #game-over {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: white;
            padding: 30px 40px;
            border-radius: 15px;
            box-shadow: 0 0 25px rgba(0,0,0,0.2);
            text-align: center;
            z-index: 100;
        }
        #restart-btn {
            background: #4CAF50;
            color: white;
            border: none;
            padding: 12px 25px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 16px;
            margin-top: 15px;
            transition: all 0.3s;
        }
        #restart-btn:hover {
            background: #45a049;
            transform: scale(1.05);
        }
        .overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            z-index: 99;
            display: none;
        }
        #game-over h2 {
            margin: 0 0 15px 0;
            color: #333;
        }
        #final-score {
            font-size: 24px;
            color: #f44336;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div id="score">得分: 0</div>
    <canvas id="game" width="300" height="600"></canvas>
    
    <!-- 游戏结束弹窗 -->
    <div id="game-over">
        <h2>游戏结束！</h2>
        <p>最终得分: <span id="final-score">0</span></p>
        <button id="restart-btn">重新开始</button>
    </div>
    <div id="overlay" class="overlay"></div>

<script>
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const BLOCK_SIZE = 30;
const COLS = 10;
const ROWS = 20;
let score = 0;
let gameInterval;

// 方块形状
const SHAPES = [
    [[1,1,1,1]], // I
    [[1,1,1],[0,1,0]], // T
    [[1,1,1],[1,0,0]], // L
    [[1,1,1],[0,0,1]], // J
    [[1,1],[1,1]], // O
    [[1,1,0],[0,1,1]], // S
    [[0,1,1],[1,1,0]]  // Z
];

// 方块颜色
const COLORS = [
    '#00f0f0', // I 青色
    '#f0a000', // T 橙色
    '#f0f000', // L 黄色
    '#a000f0', // J 紫色
    '#f00000', // O 红色
    '#00f000', // S 绿色
    '#0000f0'  // Z 蓝色
];

let board = Array.from({length: ROWS}, () => Array(COLS).fill(0));
let currentPiece = null;
let gameOver = false;

// DOM元素
const gameOverModal = document.getElementById('game-over');
const overlay = document.getElementById('overlay');
const restartBtn = document.getElementById('restart-btn');
const finalScore = document.getElementById('final-score');

function createPiece() {
    const type = Math.floor(Math.random() * SHAPES.length);
    return {
        shape: SHAPES[type],
        color: COLORS[type],
        x: Math.floor(COLS/2) - Math.floor(SHAPES[type][0].length/2),
        y: 0
    };
}

function drawBlock(x, y, color) {
    ctx.fillStyle = color;
    ctx.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE-1, BLOCK_SIZE-1);
    ctx.strokeStyle = '#ddd';
    ctx.strokeRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}

function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    
    // 绘制棋盘
    for (let y = 0; y < ROWS; y++) {
        for (let x = 0; x < COLS; x++) {
            if (board[y][x]) {
                drawBlock(x, y, board[y][x]);
            }
        }
    }
    
    // 绘制当前方块
    if (currentPiece) {
        currentPiece.shape.forEach((row, dy) => {
            row.forEach((value, dx) => {
                if (value) {
                    drawBlock(currentPiece.x + dx, currentPiece.y + dy, currentPiece.color);
                }
            });
        });
    }
}

function collision() {
    return currentPiece.shape.some((row, dy) => 
        row.some((value, dx) => {
            if (!value) return false;
            const newX = currentPiece.x + dx;
            const newY = currentPiece.y + dy;
            return (
                newX < 0 ||
                newX >= COLS ||
                newY >= ROWS ||
                board[newY]?.[newX]
            );
        })
    );
}

function merge() {
    currentPiece.shape.forEach((row, dy) => {
        row.forEach((value, dx) => {
            if (value && currentPiece.y + dy < ROWS) {
                board[currentPiece.y + dy][currentPiece.x + dx] = currentPiece.color;
            }
        });
    });
}

function rotate() {
    const shape = currentPiece.shape;
    const newShape = shape[0].map((_, i) => 
        shape.map(row => row[i]).reverse()
    );
    const oldShape = currentPiece.shape;
    currentPiece.shape = newShape;
    if (collision()) {
        currentPiece.shape = oldShape;
    }
}

function clearLines() {
    let linesCleared = 0;
    for (let y = ROWS - 1; y >= 0; y--) {
        if (board[y].every(cell => cell)) {
            board.splice(y, 1);
            board.unshift(Array(COLS).fill(0));
            linesCleared++;
            y++;
        }
    }
    if (linesCleared) {
        score += linesCleared * 100;
        document.getElementById('score').textContent = `得分: ${score}`;
    }
}

function update() {
    if (gameOver) return;
    
    if (!currentPiece) {
        currentPiece = createPiece();
        if (collision()) {
            gameOver = true;
            showGameOver();
            return;
        }
    }
    
    currentPiece.y++;
    if (collision()) {
        currentPiece.y--;
        merge();
        clearLines();
        currentPiece = null;
    }
    
    draw();
}

function resetGame() {
    board = Array.from({length: ROWS}, () => Array(COLS).fill(0));
    score = 0;
    gameOver = false;
    currentPiece = null;
    document.getElementById('score').textContent = `得分: 0`;
    gameOverModal.style.display = 'none';
    overlay.style.display = 'none';
    
    clearInterval(gameInterval);
    gameInterval = setInterval(update, 1000);
    draw();
}

function showGameOver() {
    finalScore.textContent = score;
    gameOverModal.style.display = 'block';
    overlay.style.display = 'block';
    clearInterval(gameInterval);
}

// 事件监听
restartBtn.addEventListener('click', resetGame);

document.addEventListener('keydown', event => {
    if (gameOver) return;
    
    switch(event.keyCode) {
        case 37: // 左箭头
            currentPiece.x--;
            if (collision()) currentPiece.x++;
            break;
        case 39: // 右箭头
            currentPiece.x++;
            if (collision()) currentPiece.x--;
            break;
        case 40: // 下箭头
            currentPiece.y++;
            if (collision()) {
                currentPiece.y--;
                merge();
                clearLines();
                currentPiece = null;
            }
            break;
        case 38: // 上箭头
            rotate();
            break;
        case 82: // R键
            resetGame();
            break;
    }
    draw();
});

// 初始化游戏
resetGame();
</script>
</body>
</html>